package com.blb.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.blb.entity.*;
import com.blb.entity.page.BlbPage;
import com.blb.entity.page.DataTablePage;
import com.blb.service.ICategoriesService;
import com.blb.service.ICustomerService;
import com.blb.utils.ShiroUtils;
import com.blb.utils.UUIDUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RequiresPermissions("customer2")
@Controller
@RequestMapping("/customer")
public class CustomerController {

    private static User user;


    @Resource
    private ICustomerService customerService;

    @Resource
    private ICategoriesService categoriesService;


    @RequestMapping("{page}")
    public String welcome2(@PathVariable String page){
        return "customer/"+page;
    }

    @ResponseBody
    @PostMapping("page")
    public BlbPage page( String companyProvince, String companyCity, String companyArea, String companyName, int start, int length) {
        String companyId = ShiroUtils.getUser().getCompanyId();
        List<Relation> relations = customerService.CustomerCompany(start, length, companyId, companyProvince, companyCity, companyArea, companyName);
        int count = customerService.count(companyId,companyProvince, companyCity, companyArea, companyName);
        return new DataTablePage(count, count, relations);
    }

    @RequestMapping("detailcustomer")
    public String findByCustomerId(Model model,String customerId){
        user = ShiroUtils.getUser();
        String supplierId = user.getCompanyId();
        Relation allrelation=customerService.findByCompanyId(customerId);
        model.addAttribute("companyId",customerId);
        model.addAttribute("relations",allrelation);
        List<Map<String, Object>> OrderList = customerService.findAllOrderList(customerId,supplierId);
        //System.out.println(OrderList);
        model.addAttribute("OrderList",OrderList);
        return "customer/my_clientele_xiqi.html";
    }


    @RequestMapping("detail1customer")
    public String findByCustomerId1(Model model, String customerId){
        Relation allrelation=customerService.findByCompanyId(customerId);
        model.addAttribute("allrelation",allrelation);
        model.addAttribute("companyId",customerId);
        //System.out.println(allrelation);
        return  "customer/untitl.html";
    }


    @RequestMapping("detail2customer")
    public String findByCustomerId2(Model model, String customerId){
        Relation allrelation=customerService.findByCompanyId(customerId);
        model.addAttribute("allrelation",allrelation);
        model.addAttribute("companyId",customerId);
     /*   System.out.println(allrelation);*/
        return  "customer/clientele_details.html";
    }

    @RequestMapping("detail3customer")
    public String findByCustomerId3(Model model, String customerId){
        user = ShiroUtils.getUser();
        Relation allrelation=customerService.findByCompanyId(customerId);
        int count = customerService.count(new QueryWrapper<Relation>().eq("customer_id", customerId).eq("supplier_id", user.getCompanyId()));
        int count2 = customerService.count(new QueryWrapper<Relation>().eq("customer_id", user.getCompanyId()).eq("supplier_id", customerId));
        model.addAttribute("allrelation",allrelation);
        model.addAttribute("companyId",customerId);
        model.addAttribute("count",count);
        model.addAttribute("count2",count2);

        return  "customer/clientele_cooperation.html";
    }





   @ResponseBody
   @RequestMapping("detailcompany")
   public BlbPage page(Model model, String companyName,String companyBrand,int start,int length){
       String companyId = ShiroUtils.getUser().getCompanyId();
       List<Company> companies = customerService.allCompany(start, length, companyId, companyName, companyBrand);
       model.addAttribute("companyId",companyId);
       int count2=customerService.count2(companyId,companyName,companyBrand);
       return new DataTablePage(count2, count2, companies);
   }




    @ResponseBody
    @RequestMapping("updateCompany")
    public void updateCompany(String rRemark, String companyId){
        customerService.updateCompany(rRemark, companyId);
    }



    @RequestMapping("addCustomer")
    public String addCustomer(Company company, Relation relation) {
        User user = ShiroUtils.getUser();
        company.setId(UUIDUtils.getId());
        System.out.println(company);
        System.out.println(user.getCompanyId());
        customerService.addCustomer(company);
        String customerId = company.getId();
        relation.setId(UUIDUtils.getId());
        relation.setCustomerId(customerId);
        relation.setSupplierId(user.getCompanyId());
        customerService.addRelation(relation);
        return "/customer/my_clientele_bak.html";

    }

    @ResponseBody
    @RequestMapping("addPar")
    public void addPar(Par par,@RequestParam(value = "abc[]") String[] abc){
        int a = 1;
        String id = "";
        //System.out.println(abc.toString());
        for (String s:abc){
            if(a==1){
                id = s;
                a++;
            }else if (abc.length>3){
                return;
            }
            else {
                par.setTParRel(s);
            }
        }
        par.setId(UUIDUtils.getId());
        par.setTParStatus("已申请");
        par.setTParStatus2("未查看");
        User user = ShiroUtils.getUser();
        // User user = (User) session.getAttribute("user");
        par.setTParComA(user.getCompanyId());
        Date date = new Date();
        String dateStr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
        //System.out.println(date);
        par.setTParTime(dateStr);
        par.setTParComB(id);
        //System.out.println(par);
        customerService.addPar(par);
       // System.out.println(id);

       // boolean save = customerService.save(par);

    }


    /**
     * 企业对应产品的详细信息（三级菜单）
     * @param nodeId
     * @param companyId
     * @param start
     * @param length
     * @return
     */
    @ResponseBody
    @RequestMapping("productPage")
    public BlbPage page(String nodeId, String companyId, int start, int length){
       // System.out.println(companyId);
        //System.out.println(nodeId);

        List<Categories> allNode = categoriesService.getAllNode(nodeId, companyId);
       // System.out.println(allNode);
        //如果他为最小的子节点，没有父id
        List<String> sIds=new ArrayList<>();

        if(allNode.isEmpty()){
            sIds.add(nodeId);
            List<Product> scp = customerService.getCustomerCompanyProductInfo(sIds, companyId, start, length);
           // System.out.println(scp);
            int count = customerService.count3(sIds,companyId);
            return new DataTablePage(count, count, scp);
        }

        List<String> ids=new ArrayList<>();
        for(Categories c:allNode){
            getChildrens(c,ids);
        }
        System.out.println(ids);
        List<Product> customerCompanyProductInfo = customerService.getCustomerCompanyProductInfo(ids, companyId, start, length);
        System.out.println(customerCompanyProductInfo);


        int count = customerService.count3(ids,companyId);
        System.out.println(count);

        return new DataTablePage(count, count, customerCompanyProductInfo);
    }

    public void getChildrens(Categories categories, List<String> list){
        list.add(categories.getId());
        if (categories.getProducts()!=null) {
            for (Categories c : categories.getProducts()) {
                getChildrens(c, list);
            }
        }
    }

}

